package com.zhang.chatcenter.manager.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.zhang.chatcenter.common.api.authority.UserRemote;
import com.zhang.chatcenter.common.model.dao.RoomDO;
import com.zhang.chatcenter.common.model.dao.RoomMessageDO;
import com.zhang.chatcenter.common.model.dao.RoomUserDO;
import com.zhang.chatcenter.common.model.dto.RoomMessageDTO;
import com.zhang.chatcenter.common.model.enums.RoomTypeEnums;
import com.zhang.chatcenter.dao.RoomUserMapper;
import com.zhang.chatcenter.manager.IChatManager;
import com.zhang.chatcenter.service.chat.IRoomService;
import com.zhang.infrastructure.api.authority.UserRemoteVo;
import com.zhang.infrastructure.util.UserUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * description
 *
 * @author zhangFanJun
 * @date 2024-03-19 15:36
 **/
@RequiredArgsConstructor
@Slf4j
@Service
public class ChatManager implements IChatManager {

    private final IRoomService roomService;
    private final RoomUserMapper roomUserMapper;
    private final UserRemote userRemote;

    @Override
    public List<RoomDO> getMyChat(Boolean groupChat) {

        List<RoomDO> myChat = roomService.getMyChat(groupChat);
        if(CollectionUtils.isEmpty(myChat)){
            return Collections.emptyList();
        }
        // 获取单人聊天室的用户
        List<Long> singleRoomIdList = myChat.stream()
                .filter(x -> Objects.equals(x.getType(), RoomTypeEnums.SINGLE.getType()))
                .map(x -> x.getId())
                .collect(Collectors.toList());

        List<RoomUserDO> roomUserList = new LambdaQueryChainWrapper<>(roomUserMapper)
                .in(RoomUserDO::getRoomId,singleRoomIdList)
                .list();
        if(!CollectionUtils.isEmpty(roomUserList)){
            Map<Long, Long> roomUserIdMap = roomUserList.stream()
                    .filter(x -> !Objects.equals(x.getUserId(), UserUtil.getUserId()))
                    .collect(Collectors.toMap(k -> k.getRoomId(), v -> v.getUserId(), (k1, k2) -> k1));
            Collection<Long> userIdList = roomUserIdMap.values();
            Map<Long, UserRemoteVo> userMap = userRemote.getUserByIds(new ArrayList<>(userIdList));
            if(!CollectionUtils.isEmpty(userMap)){
                for (RoomDO one : myChat) {
                    Long singleId = roomUserIdMap.get(one.getId());
                    if(Objects.nonNull(singleId)){
                        UserRemoteVo userVo = userMap.get(singleId);
                        if(Objects.nonNull(userVo)){
                            one.setName(userVo.getUsername());
                        }
                    }
                }
            }
        }

        return myChat;
    }

    @Override
    public List<RoomMessageDTO> getRoomContent(Long roomId) {

        List<RoomMessageDO> roomContents = roomService.getRoomContent(roomId);
        if(CollectionUtils.isEmpty(roomContents)){
            return Collections.emptyList();
        }
        // 获取用户信息
        Map<Long, UserRemoteVo> userMap = new HashMap<>(0);
        Set<Long> userIdSet = roomContents.stream()
                .map(RoomMessageDO::getUserId)
                .filter(Objects::nonNull)
                .collect(Collectors.toSet());
        if(!CollectionUtils.isEmpty(userIdSet)){
            Map<Long, UserRemoteVo> userRemoteVoMap = userRemote.getUserByIds(new ArrayList<>(userIdSet));
            if(!CollectionUtils.isEmpty(userRemoteVoMap)){
                userMap = userRemoteVoMap;
            }
        }
        // 封装请求
        List<RoomMessageDTO> res = new ArrayList<>();
        for (RoomMessageDO one : roomContents) {
            RoomMessageDTO contentDTO = new RoomMessageDTO();
            contentDTO.setMessage(one.getMessage());
            contentDTO.setUserId(one.getUserId());
            UserRemoteVo userVo = userMap.get(one.getUserId());
            if(Objects.nonNull(userVo)){
                contentDTO.setUsername(userVo.getUsername());
            }
            res.add(contentDTO);
        }
        return res;
    }
}
